window.onload = function () {
  var urlForm = getUrlParams('from')
  // 给from赋值
  document.getElementById('from').value = urlForm
  // 给isogeny赋值
  var currentRootDomain = getRootDomain(document.domain)
  var fromRootDomain = getRootDomain(urlForm)
  if (currentRootDomain === fromRootDomain) {
    document.getElementById('isogeny').value = true
  } else {
    document.getElementById('isogeny').value = ''
  }
}
/**
 * 登录
 */
function handleLogin (e) {
  e.preventDefault()
  var formDom = document.getElementById('form')
  var userAccDom = document.getElementById('userAcc')
  var showPwdDom = document.getElementById('showPwd')
  var errorDom = document.getElementById('error')
  if (!userAccDom.value) {
    return errorDom.textContent = '请输入用户名'
  }
  if (!showPwdDom.value) {
    return errorDom.textContent = '请输入密码'
  }
  handleEncodePwd()
  formDom.submit()
}
/**
 * 获取URL的参数
 * @param {string} name 参数名
 * @returns 返回参数值
 */
function getUrlParams (name) {
  var paramsStr = window.location.search.slice(1)
  var paramsArr = paramsStr.split('&')
  var obj = {}
  for (var i = 0; i < paramsArr.length; i++) {
    var paramStr = paramsArr[i]
    var paramArr = paramStr.split('=')
    // 这里避免参数里面是url并且带了query参数的，例如：http://localhost:8080/login.html?from=http://boss.kaver.com?abc=123这里的abc=123
    obj[paramArr[0]] = paramStr.slice(paramArr[0].length + 1)
  }
  return obj[name] || ''
}
/**
 * 使用RSA加密密码
 */
function handleEncodePwd () {
  var publicKey = document.getElementById('publicKey').value
  var showPwd = document.getElementById('showPwd').value
  var encrypt = new JSEncrypt()
  encrypt.setPublicKey('-----BEGIN PUBLIC KEY-----' + publicKey + '-----END PUBLIC KEY-----');
  var encodePwd = encrypt.encrypt(showPwd)
  document.getElementById('password').value = encodePwd
}
/**
 * 获取url的顶级域名（只获取域名的最后一个.的前后一个，可能会获取到com.cn这种，暂时不管）
 * @param {*} url 需要被获取的url
 * @returns 获取的顶级域名
 */
function getRootDomain (url) {
  return url.split('.').slice(-2).join('.')
}